package org.lt.cj.model.timer;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.lt.cj.translate.Translator;

/**
 * 
 * @author zgh
 */
public class TranslateTimer {

    private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    Translator tran = new Translator();

    public void executeTransferTimer() {

        Runnable task = new Runnable() {

            public void run() {
                tran.translateGo();
                //插入操作代码

                System.out.println("*******翻译数据完成。*******");
            }
        };
        if (scheduler.isShutdown()) {
            scheduler = Executors.newScheduledThreadPool(1);
            scheduler.scheduleAtFixedRate(task, 0, 2, TimeUnit.SECONDS);
        } else {
            scheduler.scheduleAtFixedRate(task, 0, 2, TimeUnit.SECONDS); // 延迟0秒，每隔2秒操作一次
        }
    }

    public void stop() {
        scheduler.shutdown();
    }

    public static void main(String[] args) {

        TranslateTimer tt = new TranslateTimer();
        tt.executeTransferTimer();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
        }
        tt.scheduler.shutdown();

    }
}
